package com.derive.education_service.web.admin.controller;

import com.derive.education_service.common.global.ResponseMessage;
import com.derive.education_service.web.admin.bean.dto.RoleAddDto;
import com.derive.education_service.web.admin.bean.dto.RoleUpDto;
import com.derive.education_service.web.admin.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;

@RestController
@Validated
@Slf4j
@CrossOrigin
@RequestMapping("role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    /**
     * 新增角色权限 -- 超管
     * @param request
     * @param roleAddDto
     * @return
     */
    @PostMapping("addRole")
    public ResponseMessage addRole(HttpServletRequest request, @Validated @RequestBody RoleAddDto roleAddDto) {
        return roleService.addRole(request, roleAddDto);
    }

    /**
     * 删除角色权限 -- 删除之前先判断是否存在用户具备该角色且超管不能删除 -- 超管
     * @param request
     * @param roleId
     * @return
     */
    @PostMapping("delRole")
    public ResponseMessage delRole(HttpServletRequest request, @NotNull(message = "删除id不能为空") int roleId) {
        return roleService.delRole(request, roleId);
    }

    /**
     * 查询平台角色信息 -- 不分页
     * @param request
     * @return
     */
    @GetMapping("getRoleList")
    public ResponseMessage getRoleList(HttpServletRequest request) {
        return roleService.getRoleList(request);
    }

    /**
     * 编辑平台角色信息 -- 描述信息
     * @param request
     * @param roleUpDto
     * @return
     */
    @PostMapping("upRole")
    public ResponseMessage upRole(HttpServletRequest request, @Validated @RequestBody RoleUpDto roleUpDto) {
        return roleService.upRole(request, roleUpDto);
    }

    /**
     * 获取当前账号角色权限
     * @param request
     * @return
     */
    @GetMapping("getAdminRole")
    public ResponseMessage getAdminRole(HttpServletRequest request) {
        return roleService.getAdminRole(request);
    }
}
